﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Configuration;
using System.Windows.Forms;
using System.Xml.Linq;

namespace DllSearcher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


          

            /*
                        AppSettingsReader ar = new AppSettingsReader();
    
                        Console.WriteLine("Here is your conn string");
                Console.WriteLine(ar.GetValue("appConStr", typeof(string)));
                Console.WriteLine();
                int numbOfTimes = (int)ar.GetValue("intValue", typeof(int));
                for(int i = 0; i < numbOfTimes; i++)
                  Console.WriteLine("Yo!");
              }


                        Assembly SampleAssembly;
                        // Instantiate a target object.
                        Int32 Integer1 = new Int32();
                        Type Type1;
                        // Set the Type instance to the target class type.
            //            Type1 = Integer1.GetType();
            //            // Instantiate an Assembly class to the assembly housing the Integer type.  
            //            SampleAssembly = Assembly.LoadFile("");
                        string temp = System.Configuration.ConfigurationSettings.AppSettings.Get("mykey");*/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XDocument document = XDocument.Load(openFileDialog1.FileName);
            var location = document.Descendants().Where(d => d.Attribute("assembly") != null).Select(s => s.Attribute("assembly").Value.Split(new string[1]{","},StringSplitOptions.None));
            string folderwithoutext = openFileDialog1.FileName.Remove(openFileDialog1.FileName.LastIndexOf("\\"));
            new DirectoryInfo(folderwithoutext).CreateSubdirectory("bincopy");
            foreach (var VARIABLE in location)
            {
            
                if (System.IO.File.Exists(string.Format("{0}\\{1}.dll", folderBrowserDialog1.SelectedPath, VARIABLE[0])))
                {
                    var getfile = Assembly.LoadFrom(string.Format("{0}\\{1}.dll",folderBrowserDialog1.SelectedPath,VARIABLE[0]));

                 var gettoken =   getfile.GetName().GetPublicKeyToken().ToStringBase16();
                    string replace = VARIABLE[3].Replace("PublicKeyToken=", "");
                    if (VARIABLE[3].Contains(gettoken))
                    {

                        listBox1.Items.Add(getfile.GetName());
                        System.IO.File.Copy(string.Format("{0}\\{1}.dll", folderBrowserDialog1.SelectedPath, VARIABLE[0]), string.Format("{0}\\bincopy\\{1}.dll", folderwithoutext, VARIABLE[0]));

                    }
                }
                  
            }
       

//  WebConfigSettings webConfig = new WebConfigSettings( openFileDialog1.FileName);
//  var getlist = webConfig.Get("assemblies");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
        }
    }

    public static class Extensions
    {
        // A small helper extension for Byte-Array-To-Hex
        public static string ToStringBase16(this byte[] buffer)
        {
            return buffer.Aggregate(string.Empty,
                   (result, item) => (result += item.ToString("X2").ToLower()));
        }
    }
}

